from django.db import models

# Create your models here.
# 数据库尽可能简单，不用过多复杂的逻辑，例如性别的choices
class Person(models.Model):

    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=30,verbose_name="用户名")
    # 电话号码格式建议要用CharField，而不是IntegerField
    age = models.IntegerField(max_length=10)
    birthday = models.DateTimeField(auto_now=True)

    gender = models.CharField(max_length=3,choices=(("1","男"),("2","女")))

    def __str__(self):
        return "用户id：%s,用户名：%s,年龄：%s,出生日期：%s,性别：%s "%(self.id,self.name,self.age,self.birthday,self.gender)


class Press(models.Model):
    id = models.AutoField(primary_key=True)
    pressname = models.CharField(max_length=50)
    pressaddress = models.CharField(max_length=50)

    def __str__(self):
        return "出版社名称：%s,出版社地址：%s" % (self.pressname,self.pressaddress)


class Book(models.Model):
    # create table book(id int(5) auto_increment,name varchar(20),
    # constraint fk_gid foreign key(gid) references t_group(id))
    id = models.AutoField(primary_key=True)
    bookname = models.CharField(max_length=50)
    bookprice = models.DecimalField(max_digits=10, decimal_places=2)
    bookauthor = models.CharField(max_length=30)
    # 图书图片迭代开发：第一步：添加字段，保存图书图片路径
    bookimg = models.CharField(max_length=255)
    # 级联/set null
    # set null 父表update / delete记录时，将子表上匹配记录的列设为null
    # 级联删除是指在数据库中，当删除主表中的记录时，所有与之关联的子表记录也会被自动删除。
    # 外键追加关联名称：related_name="books"
    press = models.ForeignKey(related_name="books",to="Press",on_delete=models.SET_NULL,null=True,blank=True)

    kucun = models.IntegerField(max_length=10,null=True,blank=True)
    sale = models.IntegerField(max_length=10,null=True,blank=True)

    def __str__(self):
        return "用户id：%s,图书名称：%s,图书价格：%s,图书作者：%s" % (self.id, self.bookname, self.bookprice, self.bookauthor)


class Booktype(models.Model):
    id = models.AutoField(primary_key=True)
    typename = models.CharField(max_length=50)
    books = models.ManyToManyField(to="Book",related_name="booktypes")













